<?php

namespace app\community\controller;

use app\community\model\ActiveModel;
use think\admin\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Active extends Controller
{
    /**
     * 当前操作数据库
     * @var string
     */
    private string $table = 'Active';

    /**
     * 社圈活动管理
     * @auth true
     * @menu true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $this->title = '社圈活动管理';
        $query = $this->_query(ActiveModel::class);
        // 加载对应数据
        $this->typeOf = $this->request->get('typeOf', 'index');
        if ($this->typeOf == 'index') $query->where(['is_deleted' => 0]);
        elseif ($this->typeOf == 'is_deleted') $query->where(['is_deleted' => 1]);
        $query->like('title,content')->equal('type')->order('id desc')->page(true, true);
    }

    /**
     * 内容页面添加
     * @auth true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function add()
    {

        $this->title = '发布活动';
        $this->__sysdata('page_form', 'javascript:history.back()');
    }

    /**
     * 内容页面编辑
     * @auth true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function pageEdit()
    {
        $this->title = input('type') ?? '';
        $this->__sysdata('page_form', 'javascript:history.back()');
    }

    /**
     * 显示并保存数据
     * @param string $template 模板文件
     * @param string $history 跳转处理
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    private function __sysdata(string $template, $history = '')
    {
        $id = $this->request->get('id');
        if ($this->request->isGet()) {
            $model = new ActiveModel();
            $info = $model->where(['is_deleted' => 0, 'id' => $id])->find();
            if ($info) {
                $info['time'] = date("Y-m-d H:i:s", $info['time']);
                $info['end_time'] = date("Y-m-d H:i:s", $info['end_time']);
            }
            $agreement = [
                'data' => $info
            ];
            $this->fetch($template, $agreement);
        }
        if ($this->request->isPost()) {
            if (is_string(input('data'))) {
                $data = json_decode(input('data'), true) ?: [];
            } else {
                $data = $this->request->post();
            }
            $model = new ActiveModel();
            $data['time'] = strtotime($data['time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['update_time'] = time();
            if ($id) {
                if ($model->where(['is_deleted' => 0, 'id' => $id])->update($data) !== false) {
                    $this->success('内容保存成功！', $history);
                } else {
                    $this->error('内容保存失败，请稍候再试!');
                }
            } else {
                $data['create_time'] = time();
                if ($model->insert($data) !== false) {
                    $this->success('内容保存成功！', $history);
                } else {
                    $this->error('内容保存失败，请稍候再试!');
                }
            }

        }
    }

    /**
     * 表单数据处理
     * @param array $data
     */
    protected function _form_filter(array &$data)
    {

    }

    /**
     * 修改发布状态
     * @auth true
     * @throws DbException
     */
    public function state()
    {
        $this->_applyFormToken();
        $this->_save(ActiveModel::class, $this->_vali([
            'is_deleted.in:0,1' => '状态值范围异常！',
            'is_deleted.require' => '状态值不能为空！',
        ]));
    }


    /**
     * @throws DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        $this->_save(ActiveModel::class, $this->_vali([
            'is_deleted.in:0,1' => '状态值范围异常！',
            'is_deleted.require' => '状态值不能为空！',
        ]));
    }
}